package org.ftc.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;

import org.ftc.domain.Users;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@Component
public class ServlerUtil {
		
	public HttpServletRequest getRequest(){
		return ((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes()).getRequest();
	}	
	
	public String getIP(){
		if (getRequest().getHeader("x-forwarded-for") == null) {
			return getRequest().getRemoteAddr();
		}
	    return getRequest().getHeader("x-forwarded-for");
	}
	
	public String getBrowser(){
		String userAgent = getRequest().getHeader("User-Agent");
		String browser = "unknown"; 
        if(userAgent.indexOf("MSIE")>0)
        	browser = "IE";    
        else if(userAgent.indexOf("Firefox")>0) 
        	browser = "Firefox";    
        else if(userAgent.indexOf("Chrome")>0) 
        	browser = "Chrome";    
        else if(userAgent.indexOf("Safari")>0) 
        	browser = "Safari";    
        else if(userAgent.indexOf("Camino")>0) 
        	browser = "Camino";    
        else if(userAgent.indexOf("Konqueror")>0) 
        	browser = "Konqueror";   
		return browser;
	}
	
	public String getCompany(){
		String userAgent = getRequest().getHeader("User-Agent");
		if (userAgent.indexOf("msie") > -1) {
			return "Microsoft";
		} else if (userAgent.indexOf("opera") > -1) {
			return "Opera Software";
		} else if (userAgent.indexOf("mozilla") > -1) {
			return "Netscape Communications";
		} else {
			return "unknown";
		}
    }
	
	public String getUrl(){
		return getRequest().getRequestURI();
		//return getRequest().getContextPath();
	}
	
	public String stprogram(Users users) throws IOException {
		String content = "?userName=" + URLEncoder.encode(users.getUserName(), "UTF-8");
		if( users.getPassword() != null && users.getPassword().length() > 0 )
			content += "&password=" + URLEncoder.encode(users.getPassword(), "UTF-8");
		if( users.getEmail() != null && users.getEmail().length() > 0 )
			content += "&email=" + URLEncoder.encode(users.getEmail(), "UTF-8");
		URL url = new URL("https://stprogram.stpi.narl.org.tw/syn/users"+content);  
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
        conn.setRequestMethod("POST"); 
        conn.setRequestProperty("Content-Type","text/html");
        conn.setUseCaches (true); 
        conn.setDoOutput(true);
        conn.setDoInput(true);
        String json = null;
        if(conn.getResponseCode() == 200) { 
        	BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8")); 
        	String line; 
        	while ((line = rd.readLine()) != null) { 
        		json = line;
        	} 
        	rd.close(); 
        }
        return json;
    }
	
	public String narlpm(Users users) throws IOException {
		String content = "?userName=" + URLEncoder.encode(users.getUserName(), "UTF-8");
		if( users.getPassword() != null && users.getPassword().length() > 0 )
			content += "&password=" + URLEncoder.encode(users.getPassword(), "UTF-8");
		if( users.getEmail() != null && users.getEmail().length() > 0 )
			content += "&email=" + URLEncoder.encode(users.getEmail(), "UTF-8");
		URL url = new URL("https://narlpm.stpi.narl.org.tw/syn/users.htm"+content);  
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
        conn.setRequestMethod("POST"); 
        conn.setRequestProperty("Content-Type","text/html");
        conn.setUseCaches (true); 
        conn.setDoOutput(true);
        conn.setDoInput(true);
        String json = null;
        if(conn.getResponseCode() == 200) { 
        	BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8")); 
        	String line; 
        	while ((line = rd.readLine()) != null) { 
        		json = line;
        	} 
        	rd.close(); 
        }
        return json;
    }
	
	public static void main(String[] str) throws IOException{
		Users users = new Users();
		users.setUserName("nralrv0003");
		users.setPassword("nralrv00033");
		//users.setEmail("sslu@ntu.edu.tw");
		System.out.println( new ServlerUtil().narlpm(users) );
	}
}
